package demo;

import org.springframework.batch.item.ItemWriter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * Created by 王腾 on 2017/3/10.
 */
public class ModelWriter implements ItemWriter<Model> {
    public static JdbcTemplate jdbcTemplate = null;
    static {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class);
    }

    @Override
    public void write(List<? extends Model> list) throws Exception {
        String xiaomiSql = "insert into open_card_xiaomi (business_name,mobile_factory,shenzhentong_no,seid,old_seid,source,yizi,time) values (?,?,?,?,?,?,?,?)";
        String meizuSql = "insert into open_card_meizu (business_name,mobile_factory,shenzhentong_no,seid,old_seid,source,yizi,time) values (?,?,?,?,?,?,?,?)";
        for (Model model : list) {
            if ("小米".equals(model.getMobileFactory())) {
                jdbcTemplate.update(xiaomiSql, model.getBusinessName(), model.getMobileFactory(), model.getShenZhenTongNo(), model.getSeid(), model.getOldSeid(), model.getSource(), model.getYizi(), model.getTime());
            } else {
                jdbcTemplate.update(meizuSql, model.getBusinessName(), model.getMobileFactory(), model.getShenZhenTongNo(), model.getSeid(), model.getOldSeid(), model.getSource(), model.getYizi(), model.getTime());
            }
        }
    }
}
